package routes

import (
	"forum/app/gateway/internal/http"
	"forum/app/gateway/middleware"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func NewRouter() *gin.Engine {
	router := gin.Default()
	router.Use(middleware.Cors(), middleware.ErrorMiddleware())
	store := cookie.NewStore([]byte("secret"))
	router.Use(sessions.Sessions("mysession", store))
	v1 := router.Group("/api/v1")
	{
		v1.GET("ping", func(c *gin.Context) {
			c.JSON(200, "success")
		})

		// 用户服务
		v1.POST("/user/register", http.UserRegister)
		v1.POST("/user/login", http.UserLogin)

		// 登录保护
		authed := v1.Group("/")
		authed.Use(middleware.JWT())
		{
			authed.PUT("user/update", http.UserUpdate)
			authed.DELETE("user/delete", http.UserDelete)

			// 帖子服务
			authed.POST("post/create", http.PostCreate)
			authed.DELETE("post/delete", http.PostDelete)
			authed.PUT("post/update", http.PostUpdate)
			authed.GET("post/show", http.PostShow)
			authed.GET("post/show_one", http.PostShowOne)

			// 评论服务
			authed.POST("comment/create", http.CommentCreate)
			authed.PUT("comment/update", http.CommentUpdate)
			authed.DELETE("comment/delete", http.CommentDelete)
			authed.GET("comment/list", http.CommentList)
			authed.GET("comment/get", http.CommentGet)
		}
	}

	return router
}
